describe('pubsub ', function() {
    var pb;
    beforeEach(function() {
        pb = jc.initPB();
        //pb.fn.$jcQueue = {};
    })
    it("can return the Observable method", function() {
        expect(pb("a").bind).toBeDefined();
        expect(pb("a").publish).toBeDefined();
        expect(pb("a").unbind).toBeDefined();
        expect(pb("a").eventNamesWithReg).toBeDefined()
    });
    it(" the '$all' argument can get all channels", function() {
        var arr = ["a", "b", "c", "d"]
        pb(arr);
        pb("$all");
        arr.forEach(function(que) {
            expect(pb.fn.$que).toContain(que);
        })
    })

    it("can create channels except the '$all' channel", function() {
        pb('$all')
        expect(Object.keys(pb.fn.$jcQueue).length).toEqual(0);
        pb("a,b")
        expect(getType(pb.fn.$jcQueue.a)).toEqual("object")
        expect(getType(pb.fn.$jcQueue.b)).toEqual("object")

    })

    describe('Observable ', function() {
        var fn1, fn2, container = {}


        beforeEach(function() {
            fn1 = function() {};
            fn2 = function() {};
            container = {
                "fn1": fn1,
                "fn2": fn2
            };
        })
        it("can bind a function", function() {
            pb("a").bind("ea", fn1);
            var bindFns = pb.fn.$jcQueue.a.ea
            expect(getType(bindFns)).toEqual("array");
            expect(bindFns[0]).toEqual(fn1)
            expect(bindFns[0].fnName).toEqual(undefined)
            pb("b").bind("e", "fn2", fn2);
            expect(pb.fn.$jcQueue.b.e[0].fnName).toEqual("fn2")
        })
        it("can bind functions", function() {
            pb("a").bind("ea", fn1)
            pb("a").bind("ea", "fn2", fn2)
            var bindFns = pb.fn.$jcQueue.a.ea

            expect(getType(bindFns)).toEqual("array");
            [fn1, fn2].forEach(function(fn) {
                expect(bindFns).toContain(fn)
                if (fn2 === fn)
                    expect(fn.name === 'fn2')
                if (fn1 === fn) {
                    expect(fn.name === undefined)
                }
            })
        })
        it("can find eventName by regex", function() {
            pb("a").bind("e.a.v", fn1)
            pb("a").bind("e.a.agb", fn2)
            pb("a").bind("e.a", fn1)
            pb("a").bind("aie,e.a", fn1)
            expect(pb("a").eventNamesWithReg(/^e.a/)).toEqual(["e.a.v", "e.a.agb", "e.a"])
        })
        it("can pub a event", function() {
            spyOn(container, "fn1")
            spyOn(container, "fn2")
            pb("a,b").bind("e", container.fn1)
            pb("b").bind("e", container.fn2)
            var data = {};
            pb("b").publish("e", data);
            expect(container.fn2).toHaveBeenCalledWith(data, "e")
            expect(container.fn1).toHaveBeenCalledWith(data, "e")
            data = {
                a: "aa"
            }
            pb("a").publish("e", data);
            expect(container.fn1).toHaveBeenCalledWith(data, "e")

        })
        describe("can unbind", function() {
            beforeEach(function() {
                pb("a").bind("e", fn1);
                pb("a").bind("e", "fn2", fn2);
                pb("a").bind("a", fn1);
                pb("a").bind("a", "fn2", fn2);
            })
            it("a event", function() {
                pb("a").unbind("e");
                pb("a").unbind("a", "fn2")
                expect("e" in pb.fn.$jcQueue.a).toBe(false)
                expect(pb.fn.$jcQueue.a.a.length).toEqual(1)
            })
            it("all event by $all", function() {
                pb("a").unbind("$all")
                expect("a" in pb.fn.$jcQueue).toBe(false)
            })
        })

    });
});